package com.bignerdranch.android.expresshelper.Activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.bignerdranch.android.expresshelper.Bean.Tab;
import com.bignerdranch.android.expresshelper.Fragment.HistoryFragment;
import com.bignerdranch.android.expresshelper.Fragment.PhoneFragment;
import com.bignerdranch.android.expresshelper.Fragment.QueryFragment;
import com.bignerdranch.android.expresshelper.R;
import com.sortlistview.MainFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private FragmentTabHost mTabHost;
    private LayoutInflater mInflater;
    private List<Tab> mTabs = new ArrayList<>(3);

    //上次按下返回键的系统时间
    private long lastBackTime = 0;
    //当前按下返回键的系统时间
    private long currentBackTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initTab();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//捕获返回键按下的事件
       /* FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.realtabcontent);
        &&fragment.getClass()==QueryFragment.class*/

        if(keyCode == KeyEvent.KEYCODE_BACK){
            //获取当前系统时间的毫秒数
            currentBackTime = System.currentTimeMillis();
            //比较上次按下返回键和当前按下返回键的时间差，如果大于2秒，则提示再按一次退出
            if(currentBackTime - lastBackTime > 2 * 1000){
                Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
                lastBackTime = currentBackTime;
            }else{ //如果两次按下的时间差小于2秒，则退出程序
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initTab() {

        Tab query = new Tab(QueryFragment.class,R.drawable.selector_qury,R.string.query);
        Tab history = new Tab(HistoryFragment.class,R.drawable.selector_history,R.string.history);
        Tab phone = new Tab(MainFragment.class,R.drawable.selector_phone,R.string.phone);

        mTabs.add(query);
        mTabs.add(history);
        mTabs.add(phone);

        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mInflater = LayoutInflater.from(this);

        for (Tab tab:mTabs){
           TabHost.TabSpec spec = mTabHost.newTabSpec(getString(tab.getText()));

            spec.setIndicator(initIndicator(tab));
            mTabHost.addTab(spec,tab.getFragment(),null);
        }
    }
    public View initIndicator(Tab tab){
        View view = mInflater.inflate(R.layout.item_bottom,null);
        ImageView img = (ImageView) view.findViewById(R.id.icon_image);
        TextView text = (TextView) view.findViewById(R.id.icon_indicator);
        img.setImageResource(tab.getIcon());
        text.setText(tab.getText());

        return view;
    }

}
